/*
	Copyright (c) 2010, Mathias Myrland, Myrland Systemutvikling (mathias@myrsys.no)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
	
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	      
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	      
	    * Neither the name of Myrland Systemutvikling nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CGF_MANAGED_CONTAINER_HPP
#define CGF_MANAGED_CONTAINER_HPP


#include <boost/weak_ptr.hpp>
#include <cgf/signal.hpp>
#include <cgf/object.hpp>
#include <boost/bind.hpp>
#include <list>

namespace CGF
{
	
	/**
	*	A managed container class.
	*
	*	The t_stored type must be derived from SignalProducer
	*
	*	@tparam template<class> t_container		The type of the container to manage
	*	@tparam t_stored				The type of the pointers to store	*/
	template<class t_stored, template<class> class t_container = std::list>
	class ManagedContainer : public SignalProducer, Object<ManagedContainer<t_stored, t_container> >
	{
		typedef boost::weak_ptr<t_stored> ptr_w_stored;
		typedef boost::shared_ptr<t_stored> ptr_stored;
		typedef t_container<ptr_w_stored> t_storage;
		
		/**	The actual container we are managing	*/
		t_storage m_contained;
		
		public:
			
			typedef typename t_storage::iterator iterator;
		
			/**
			*	The onDestroyed notification slot. This function is connected to its managed objects destroyed signal.
			*
			*	@param	obj	The object being destroyed, which is to be removed from the container.	*/
			void onDestroyed ( SignalProducer::ptr obj )
			{
				iterator it = m_contained.begin();
				for ( ; it != m_contained.end(); it++ )
				{
					if ( !(*it).use_count() )
						break;
				}
				m_contained.erase ( it );
			}
			
			size_t size()
			{
				return m_contained.size();
			}
			/**
			*	Return the begin iterator of the storage class
			*
			*	@return	iterator to the first element of the managed list	*/
			iterator begin()
			{
				return m_contained.begin();
			}
			/**
			*	Return the end iterator of the storage class
			*
			*	@return	iterator to the last+1 element of the managed list	*/
			iterator end()
			{
				return m_contained.begin();
			}
			
			/**
			*	Pushes a value to the back of the container
			*
			*	@param	value	A shared pointer to the value to push	*/
			void push_back ( ptr_stored value )
			{
				m_contained.push_back ( ptr_w_stored ( value ) );
				boost::function<void(SignalProducer::ptr)> func = boost::bind( &ManagedContainer<t_stored, t_container>::onDestroyed, this, _1 );
				value->connect ( "destroyed",  func  );
				
				//	Signal the new entry slot
				signal ( "newEntry" );
			}
			
	};

}//	namespace CGF

#endif
